package com.dzl.goods;

import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.ApiExtractCardConstant;
import com.dzl.constant.WelfareApiTypeEnum;
import com.dzl.constant.WelfareGoodsStatusEnum;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.query.WelfareGoodsDevAreaQuery;
import com.dzl.goods.pojo.query.WelfareGoodsPageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsBaseVo;
import com.dzl.goods.pojo.vo.WelfareGoodsDetailVo;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品表(WelfareGoods)表控制层
 * @author ChenLong
 * @since 2021-05-18 10:01:46
 */
@Slf4j
@RestController
@RequestMapping("welfareGoods")
public class WelfareGoodsController {

    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private WelfareSupplierService welfareSupplierService;

    /**
     * 通过商品名称搜索商品列表
     * @param baseQuery 商品名称搜索字符
     * @return java.util.List
     * @author ChenLong
     * @since 2021-05-18 14:31:46
     **/
    @GetMapping(value = "getGoodsListByNameLike")
    public JsonResult<List<WelfareGoodsBaseVo>> getGoodsListByNameLike(WelfareGoodsPageQuery baseQuery) {
        log.info("getGoodsListByNameLike in ");
        if (EmptyUtils.isEmpty(baseQuery) || EmptyUtils.isEmpty(baseQuery.getQueryStr())) {
            return JsonResult.paramErrWithMsg("无效参数");
        }
        // 去空格
        baseQuery.setQueryStr(baseQuery.getQueryStr().replaceAll(" ", ""));
        baseQuery.setPageSize(5);
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsBaseListPage(baseQuery);
        return JsonResult.success(pageInfo.getList());
    }

    /**
     * 通过商品名称搜索商品列表
     * @param baseQuery 商品名称搜索字符
     * @return java.util.List
     * @author ChenLong
     * @since 2021-05-18 14:31:46
     **/
    @GetMapping(value = "getGoodsListByNameLikeByPage")
    public PageUtils<WelfareGoodsBaseVo> getGoodsListByNameLikeByPage(WelfareGoodsPageQuery baseQuery) {
        log.info("getGoodsListByNameLikeByPage in ");
        if (EmptyUtils.isEmpty(baseQuery) || EmptyUtils.isEmpty(baseQuery.getQueryStr())) {
            throw new CommonException("无效参数");
        }
        // 去空格
        baseQuery.setQueryStr(baseQuery.getQueryStr().replaceAll(" ", ""));
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsBaseListPage(baseQuery);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 获取商品发货信息
     * @param supplierId 供应商id
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-18 20:47:57
     **/
    @GetMapping(value = "getGoodsDeliveryInfo")
    public JsonResult<WelfareSupplier> getGoodsDeliveryInfo(Integer supplierId) {
        log.info("getGoodsDeliveryInfo in supplierId: " + supplierId);
        if (EmptyUtils.isEmpty(supplierId)) {
            return JsonResult.paramErrWithMsg("参数错误，无效的供应商");
        }
        WelfareSupplier welfareSupplier = welfareSupplierService.selectByPrimaryKey(supplierId);
        return JsonResult.success(welfareSupplier);
    }

    /**
     * 获取商品规格列表
     * @param goodsId 商品id
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-18 19:43:07
     **/
    @GetMapping(value = "getWelfareGoodsAttr")
    public JsonResult<List<WelfareGoodsAttr>> getWelfareGoodsAttr(Integer goodsId) {
        log.info("getWelfareGoodsAttr in goodsId: " + goodsId);
        if (EmptyUtils.isEmpty(goodsId)) {
            return JsonResult.paramErrWithMsg("参数错误，无效的商品信息");
        }
        List<WelfareGoodsAttr> welfareGoodsAttrList = welfareGoodsService.getWelfareGoodsAttr(goodsId);
        return JsonResult.success(welfareGoodsAttrList);
    }

    /**
     * 获取商品详细信息
     * @param type 类型；1:校验是否隐藏，2:不校验是否隐藏
     * @param goodsId 商品id
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-18 20:47:38
     **/
    @GetMapping(value = "getGoodsInfoClassifySku")
    public JsonResult<WelfareGoodsDetailVo> getGoodsInfoClassifySku(Integer goodsId, Integer type) {
        log.info("getGoodsInfoClassifySku in goodsId: " + goodsId);
        if (EmptyUtils.isEmpty(goodsId) || EmptyUtils.isEmpty(type)) {
            return JsonResult.paramErrWithMsg("参数错误，无效的商品信息");
        }
        WelfareGoodsDetailVo welfareGoodsWithImgVo = welfareGoodsService.getGoodsInfoClassifySku(goodsId);
        if (EmptyUtils.isNotEmpty(welfareGoodsWithImgVo) && !welfareGoodsWithImgVo.getGoodsStatus().equals(WelfareGoodsStatusEnum.GOODS_STATUS_TYPE_SHELVES.getCode())) {
            return JsonResult.paramErrWithMsgAndCode(1000, "商品已售罄");
        }
        // 兑换券查看商品详情不校验是否隐藏，其他地方查看商品详情需要校验是否隐藏
        if (EmptyUtils.isNotEmpty(welfareGoodsWithImgVo) && type.equals(1) && !welfareGoodsWithImgVo.getIsShow().equals(1)) {
            return JsonResult.paramErrWithMsgAndCode(1000, "商品已售罄");
        }
        if (EmptyUtils.isNotEmpty(welfareGoodsWithImgVo.getIsApi()) && welfareGoodsWithImgVo.getIsApi().equals(1)){
            // 如果供应商是亚捷，则apiType为2；如果不是亚捷，则为1
            if (EmptyUtils.isNotEmpty(welfareGoodsWithImgVo.getMerchantId()) && welfareGoodsWithImgVo.getMerchantId().equals(ApiExtractCardConstant.getMerchantId())){
                welfareGoodsWithImgVo.setApiType(WelfareApiTypeEnum.API_TYPE_EXTRACT_CARD.getCode());
            }{
                welfareGoodsWithImgVo.setApiType(WelfareApiTypeEnum.API_TYPE_RECHARGE.getCode());
            }
        }
        return JsonResult.success(welfareGoodsWithImgVo);
    }

    /**
     * 校验选择地址是否支持配送
     * @author ChenLong
     * @date 2021-11-03 14:13:36
     * @param query 商品区域信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     **/
    @GetMapping(value = "validGoodsSupplierDelivery")
    public JsonResult<Integer> validGoodsSupplierDelivery(WelfareGoodsDevAreaQuery query){
        if (EmptyUtils.isEmpty(query.getGoodsId())){
            throw new CommonException("无效商品");
        }
        if (EmptyUtils.isEmpty(query.getArea())){
            throw new CommonException("无效地区");
        }
        if (welfareGoodsService.validGoodsSupplierDelivery(query)){
            return JsonResult.success(1);
        } else {
            return JsonResult.success(2);
        }
    }

    /**
     * 根据分类获取品牌
     * @param welfareGoodsQuery 一级分类id、二级分类id、三级分类id、分区id
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-18 20:47:00
     **/
    @GetMapping(value = "getBrandListByGoodsClassify")
    public JsonResult<List<WelfareBrand>> getBrandListByGoodsClassify(WelfareGoodsPageQuery welfareGoodsQuery) {
        log.info("getBrandListByGoodsClassify welfareGoodsQuery: " + welfareGoodsQuery.toString());
        List<WelfareBrand> welfareBrandList = welfareGoodsService.getBrandListByGoodsQuery(welfareGoodsQuery);
        return JsonResult.success(welfareBrandList);
    }

    /**
     * 根据分类获取商品列表（分页）
     * @param welfareGoodsPageQuery 商品查询条件实体
     * @return com.dzl.util.PageUtils<com.dzl.goods.entity.vo.WelfareGoodsWithLowPriceVo>
     * @author ChenLong
     * @since 2021-05-20 10:26:52
     **/
    @GetMapping(value = "getGoodsListByClassifyByPage")
    public PageUtils<WelfareGoodsBaseVo> getGoodsListByClassifyByPage(WelfareGoodsPageQuery welfareGoodsPageQuery) {
        log.info("getGoodsListByClassifyByPage in welfareGoodsPageQuery: " + welfareGoodsPageQuery.toString());
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsBaseListPage(welfareGoodsPageQuery);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 根据跳转商品类型id获取商品列表（分页）
     * @param welfareGoodsPageQuery 商品查询条件实体
     * @return com.dzl.util.PageUtils<com.dzl.goods.entity.vo.WelfareGoodsWithLowPriceVo>
     * @author ChenLong
     * @since 2021-05-20 10:27:19
     **/
    @GetMapping(value = "getGoodsListByLinkGoodsByPage")
    public PageUtils<WelfareGoodsBaseVo> getGoodsListByLinkGoodsByPage(WelfareGoodsPageQuery welfareGoodsPageQuery) {
        log.info("getGoodsListByLinkGoodsByPage in welfareGoodsPageQuery: " + welfareGoodsPageQuery.toString());
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsBaseListPage(welfareGoodsPageQuery);
        return new PageUtils<>(pageInfo);
    }
}
